/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package gui;

import controlacciones.ejb3.pojo.TableBeanBinding_Progcort;
import delegate.GestionDelegate;
import helper.JP_Utiles_PanelModelo;
import helper.JP_Utiles_PanelBotonesMantenimiento;
import helper.Utiles_MensajesDialogo;
import java.awt.BorderLayout;
import org.jdesktop.application.Task;
import java.util.ArrayList;
import java.util.List;
import org.jdesktop.application.Action;

/**
 *
 * @author Vanessa
 */
public class JP_DetalleCorte extends JP_Utiles_PanelModelo {
    

    /** Creates new form JP_RegistraAccion */
    public JP_DetalleCorte( ) {        
        inicializaComponentes();
    }

    private void inicializaComponentes(){
        initComponents();
        //Panel de Operaciones
        ArrayList alBotones = new ArrayList( 1 );        
        alBotones.add( new Integer( JP_Utiles_PanelBotonesMantenimiento.BOTON_CERRAR ) );

        jp_ope = new JP_Utiles_PanelBotonesMantenimiento( this, alBotones );
        jp_Operaciones.setLayout( new BorderLayout() );
        jp_Operaciones.add( jp_ope, BorderLayout.CENTER );         
    }   

    public void setDatos( String filename ){
        this.lblNroPrograma.setText(filename);
        consultarProgramaCorte();
        getNumPaginas();
        this.lblNroPagina.setText("N° de Página(50 reg/pag):  " + pagActual);
        cargaTabla();
        this.lblNroRegistros.setText(filename);
    }

    public void cargaTabla(){
        try {
            
            listaDatos = GestionDelegate.getInstance().getListaCorte(pagActual, pagTamanno);
        } catch (Exception ex) {
            //Logger.getLogger(JP_ListarAcciones_Area.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println( "Error al listar cortes: " + ex.getMessage() );
        }

        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();
        jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, listaDatos, tabla);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding0 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${preregiox}"));
        columnBinding0.setColumnName("preregiox");
        columnBinding0.setColumnClass(String.class);
        columnBinding0.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding1 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prezonx}"));
        columnBinding1.setColumnName("Numero de prezonx");
        columnBinding1.setColumnClass(String.class);
        columnBinding1.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding2 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${presector}"));
        columnBinding2.setColumnName("presector");
        columnBinding2.setColumnClass(String.class);
        columnBinding2.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding3 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${premzn}"));
        columnBinding3.setColumnName("premzn");
        columnBinding3.setColumnClass(String.class);
        columnBinding3.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding4 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${prelote}"));
        columnBinding4.setColumnName("prelote");
        columnBinding4.setColumnClass(String.class);
        columnBinding4.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding5 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${presublote}"));
        columnBinding5.setColumnName("presublote");
        columnBinding5.setColumnClass(String.class);
        columnBinding5.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding6 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding6.setColumnName("nombre");
        columnBinding6.setColumnClass(String.class);
        columnBinding6.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding7 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${inscrinrp}"));
        columnBinding7.setColumnName("inscrinrp");
        columnBinding7.setColumnClass(String.class);
        columnBinding7.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding8 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${direccion}"));
        columnBinding8.setColumnName("direccion");
        columnBinding8.setColumnClass(String.class);
        columnBinding8.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding9 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cornroprog}"));
        columnBinding9.setColumnName("cornroprog");
        columnBinding9.setColumnClass(String.class);
        columnBinding9.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding10 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cornrolin}"));
        columnBinding10.setColumnName("cornrolin");
        columnBinding10.setColumnClass(String.class);
        columnBinding10.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding11 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${corsecuen}"));
        columnBinding11.setColumnName("corsecuen");
        columnBinding11.setColumnClass(String.class);
        columnBinding11.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding12 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${corfecelab}"));
        columnBinding12.setColumnName("corfecelab");
        columnBinding12.setColumnClass(String.class);
        columnBinding12.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding13 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${urba}"));
        columnBinding13.setColumnName("urba");
        columnBinding13.setColumnClass(String.class);
        columnBinding13.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding14 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nromedido}"));
        columnBinding14.setColumnName("nromedido");
        columnBinding14.setColumnClass(String.class);
        columnBinding14.setEditable(false);

        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding15 = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${mednror}"));
        columnBinding15.setColumnName("mednror");
        columnBinding15.setColumnClass(String.class);
        columnBinding15.setEditable(false);

        bindingGroup.addBinding(jTableBinding);
        bindingGroup.bind();
    }

    public void inicializaReferencias(){

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always re-generated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jp_Operaciones = new javax.swing.JPanel();
        jp_DatosUsuario = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        lblNroPrograma = new javax.swing.JLabel();
        lblNroRegistros = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        btnPrimero = new javax.swing.JButton();
        btnAnterior = new javax.swing.JButton();
        btnSiguiente = new javax.swing.JButton();
        btnUltimo = new javax.swing.JButton();
        lblNroPagina = new javax.swing.JLabel();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class).getContext().getResourceMap(JP_DetalleCorte.class);
        jp_Operaciones.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jp_Operaciones.border.title"))); // NOI18N
        jp_Operaciones.setName("jp_Operaciones"); // NOI18N

        javax.swing.GroupLayout jp_OperacionesLayout = new javax.swing.GroupLayout(jp_Operaciones);
        jp_Operaciones.setLayout(jp_OperacionesLayout);
        jp_OperacionesLayout.setHorizontalGroup(
            jp_OperacionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 741, Short.MAX_VALUE)
        );
        jp_OperacionesLayout.setVerticalGroup(
            jp_OperacionesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 27, Short.MAX_VALUE)
        );

        jp_DatosUsuario.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jp_DatosUsuario.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, resourceMap.getFont("jp_DatosUsuario.border.titleFont"))); // NOI18N
        jp_DatosUsuario.setName("jp_DatosUsuario"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tabla.setName("tabla"); // NOI18N
        jScrollPane1.setViewportView(tabla);

        lblNroPrograma.setFont(resourceMap.getFont("lblNroPrograma.font")); // NOI18N
        lblNroPrograma.setText(resourceMap.getString("lblNroPrograma.text")); // NOI18N
        lblNroPrograma.setName("lblNroPrograma"); // NOI18N

        lblNroRegistros.setForeground(resourceMap.getColor("lblNroRegistros.foreground")); // NOI18N
        lblNroRegistros.setText(resourceMap.getString("lblNroRegistros.text")); // NOI18N
        lblNroRegistros.setName("lblNroRegistros"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jPanel1.setName("jPanel1"); // NOI18N

        btnPrimero.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/images/2leftarrow.gif"))); // NOI18N
        btnPrimero.setName("btnPrimero"); // NOI18N
        btnPrimero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrimeroActionPerformed(evt);
            }
        });

        btnAnterior.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/images/1leftarrow.gif"))); // NOI18N
        btnAnterior.setName("btnAnterior"); // NOI18N
        btnAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAnteriorActionPerformed(evt);
            }
        });

        btnSiguiente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/images/1rightarrow.gif"))); // NOI18N
        btnSiguiente.setName("btnSiguiente"); // NOI18N
        btnSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSiguienteActionPerformed(evt);
            }
        });

        btnUltimo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gui/resources/images/2rightarrow.gif"))); // NOI18N
        btnUltimo.setName("btnUltimo"); // NOI18N
        btnUltimo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUltimoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(btnPrimero, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAnterior, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSiguiente, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnUltimo, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnAnterior, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnSiguiente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnUltimo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(btnPrimero, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
        );

        lblNroPagina.setForeground(resourceMap.getColor("lblNroPagina.foreground")); // NOI18N
        lblNroPagina.setText(resourceMap.getString("lblNroPagina.text")); // NOI18N
        lblNroPagina.setName("lblNroPagina"); // NOI18N

        javax.swing.GroupLayout jp_DatosUsuarioLayout = new javax.swing.GroupLayout(jp_DatosUsuario);
        jp_DatosUsuario.setLayout(jp_DatosUsuarioLayout);
        jp_DatosUsuarioLayout.setHorizontalGroup(
            jp_DatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DatosUsuarioLayout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(lblNroPrograma, javax.swing.GroupLayout.DEFAULT_SIZE, 731, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 751, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jp_DatosUsuarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jp_DatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblNroRegistros, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblNroPagina, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 322, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jp_DatosUsuarioLayout.setVerticalGroup(
            jp_DatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jp_DatosUsuarioLayout.createSequentialGroup()
                .addComponent(lblNroPrograma)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jp_DatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jp_DatosUsuarioLayout.createSequentialGroup()
                        .addComponent(lblNroRegistros)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblNroPagina))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jp_DatosUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jp_Operaciones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jp_DatosUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jp_Operaciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnPrimeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrimeroActionPerformed
        this.primero();
        this.cargaTabla();
}//GEN-LAST:event_btnPrimeroActionPerformed

    private void btnAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAnteriorActionPerformed
        this.anterior();
        this.cargaTabla();
}//GEN-LAST:event_btnAnteriorActionPerformed

    private void btnSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSiguienteActionPerformed
        this.siguiente();
        this.cargaTabla();
}//GEN-LAST:event_btnSiguienteActionPerformed

    private void btnUltimoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUltimoActionPerformed
        this.ultimo();
        this.cargaTabla();
}//GEN-LAST:event_btnUltimoActionPerformed
    
    @Override
    public Task cerrar() {
        this.cerrarPadre();
        return null;
    }

    public void primero(){
        pagActual = 0;
        System.out.println("pagActual: " + pagActual);
        this.lblNroPagina.setText("N° de Página(50 reg/pag):  " + pagActual);
    }

    public void siguiente() {
        if( pagActual < this.pagUltima){
            pagActual++;
        }
        this.lblNroPagina.setText("N° de Página(50 reg/pag):  " + pagActual);
    }

    public void anterior() {
        pagActual--;
        if (pagActual < 0) {
            pagActual = 0;
        }
        this.lblNroPagina.setText("N° de Página(50 reg/pag):  " + pagActual);
    }

    public void ultimo(){
        pagActual = pagUltima;
        System.out.println("pagActual: " + pagActual);
        this.lblNroPagina.setText("N° de Página(50 reg/pag):  " + pagActual);
    }

    public void getNumPaginas(){
        try {
            numRegistros = getNumRegistros();
            if (pagTamanno > 0) {
                double ultima = numRegistros / pagTamanno;
                System.out.println("ultima:" + ultima);
                pagUltima = (int) java.lang.Math.ceil(ultima);
                System.out.println("pagUltima: " + pagUltima);
            }
        } catch (Exception e) {
        }
        System.out.println("pagUltima: " + pagUltima);
    }

    public int getNumRegistros(){
        int num = 0;
        try {
            num = GestionDelegate.getInstance().getNumRegistrosCortes();
            System.out.println( "Num de Registros: " + num );
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return num;
    }

    @Action
    public Task consultarProgramaCorte() {
        return new ConsultarProgramaCorte(org.jdesktop.application.Application.getInstance(controlaccionescomercial.ControlAccionesComercialApp.class));
    }

    private class ConsultarProgramaCorte extends org.jdesktop.application.Task<Object, Void> {
        ConsultarProgramaCorte(org.jdesktop.application.Application app) {
            super(app);
        }
        @Override protected Object doInBackground() {            
            GestionDelegate.getInstance().integraProgramaCorte(filename);
            return null;  // return your result
        }
        @Override protected void succeeded(Object result) {
            Utiles_MensajesDialogo.mensajeExito();
        }
    }    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnterior;
    private javax.swing.JButton btnPrimero;
    private javax.swing.JButton btnSiguiente;
    private javax.swing.JButton btnUltimo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel jp_DatosUsuario;
    private javax.swing.JPanel jp_Operaciones;
    private javax.swing.JLabel lblNroPagina;
    private javax.swing.JLabel lblNroPrograma;
    private javax.swing.JLabel lblNroRegistros;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables
    private JP_Utiles_PanelBotonesMantenimiento jp_ope;
    private int pagActual = 0;
    //private int maxResults;
    private int pagTamanno = 50;
    private int numRegistros = 1;
    int pagUltima = 0;
    String filename = null;
    private List<TableBeanBinding_Progcort> listaDatos;
    private org.jdesktop.swingbinding.JTableBinding jTableBinding;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
}
